<?php
define('FPDF_FONTPATH', 'font/');
require('./font/fpdf.php');
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $pdf=new FPDF('P','mm','A4');   //Create new pdf file
  $pdf->Open();     //Open file
  $pdf->SetAutoPageBreak(false);  //Disable automatic page break
  $pdf->AddPage();  //Add first page
  $i         = 0; 
  $rowHeight = 6;
  $yAxis     = 19;
  $yAxis = $yAxis + $rowHeight;
  pageHeader();
  $srNo = 0;
  $orderId       = isset($_REQUEST['orderId']) ? $_REQUEST['orderId']   : 0;
  $tableId       = isset($_REQUEST['tableId']) ? $_REQUEST['tableId']   : 0;
  $waiterId      = isset($_REQUEST['waiterId']) ? $_REQUEST['waiterId'] : 0;
  $tablePart     = isset($_REQUEST['tablePart']) ? $_REQUEST['tablePart'] : 0;
  $totalAmount   = 0;
  $totalQuantity = 0;
  $srNo = 0;
  $selectOrder = "SELECT itemName,quantity,orderTime,ordereditems.orderId,
                         waiterName,ordermaster.tableId,ordereditems.itemId,waiter.waiterId,ordermaster.tableId,
                         item.itemPrice
                    FROM ordereditems 
               LEFT JOIN ordermaster ON ordereditems.orderId = ordermaster.orderId
               LEFT JOIN item ON ordereditems.itemId = item.itemId
               LEFT JOIN waiter ON ordermaster.waiterId = waiter.waiterId
                   WHERE ordermaster.tableId = ".$tableId."
                     AND ordermaster.waiterId = ".$waiterId."
                     AND ordermaster.orderstatus = 'L'
                     AND ordermaster.tablePart = '".$tablePart."'";
  $selectOrderRes = mysql_query($selectOrder);  
  while($orderRow = mysql_fetch_array($selectOrderRes))
  {
  	$srNo++;
    $itemName       = $orderRow['itemName'];
    $itemPrice      = $orderRow['itemPrice'];
    $quantity       = $orderRow['quantity'];
    $waiterName     = $orderRow['waiterName'];
    $amount         = $orderRow['quantity'] * $orderRow['itemPrice'];
    $totalQuantity += $orderRow['quantity'];
    $totalAmount   += $amount;
    $i++;
    $pdf->SetTextColor(0,0,0);
    $pdf->SetXY(5,$yAxis + 6);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(12, 6, $srNo,   1,0,'C');
    $pdf->Cell(15, 6, $quantity,   1,0,'C');
    $pdf->Cell(37, 6, $itemName, 1,0,'L');
    $pdf->Cell(24, 6, $itemPrice,         1,0,'R');
    $pdf->Cell(22, 6, $amount,  1,0,'R');
    $yAxis += $rowHeight;
    $i++;
  }
  
  $updateOrdredItems = "UPDATE ordermaster 
                           SET orderstatus = 'C'
                         WHERE orderId = ".$orderId;
  mysql_query($updateOrdredItems);
  
  $pdf->SetTextColor(0,0,0);
  $pdf->SetXY(55,13);
  $pdf->SetFont('Arial', 'B', 15);
  $pdf->Cell(30, 10, $waiterName,   1,0,'C');
  $pdf->Cell(30, 10, $tableId,   1,0,'C');
  $pdf->SetTextColor(255,0,0);
  $pdf->SetFont('Arial', 'B', 10);
  $pdf->SetXY(5,$yAxis+6);
  $pdf->Cell(12, 6, 'TOTAL', 1, 0, 'C', 0);
  $pdf->Cell(15, 6, $totalQuantity, 1, 0, 'C', 0);
  $pdf->Cell(61, 6, '', 1, 0, 'C', 0);
  $pdf->Cell(22, 6, $totalAmount, 1, 0, 'C', 0);
  //Set Row Height
  //Create file
  $pdf->Output();
}
//header part start
function pageHeader()
{
  global $pdf;
  $pdf->Image('./logo/123.png',5,4,50,18);
  $pdf->SetXY(5,3);
  $pdf->Cell(50, 20, '', 1, 0, 'C');
  $pdf->SetFont('Times','',20);
  $pdf->SetXY(55,3);
  $pdf->Cell(30, 10, 'Waiter', 1, 0, 'C');
  $pdf->Cell(30, 10, 'Table', 1, 0, 'C');
  $pdf->SetTextColor(0,0,0);
  $pdf->SetFont('Arial', '', 10);
  $pdf->SetXY(5,25);
  $pdf->Cell(12, 6, 'Sr.No.', 1, 0, 'C', 0);
  $pdf->Cell(15, 6, 'Qty.',   1, 0, 'C', 0);
  $pdf->Cell(37, 6, 'Item.',  1, 0, 'C', 0);
  $pdf->Cell(24, 6, 'Rate',   1, 0, 'C', 0);
  $pdf->Cell(22, 6, 'Amount', 1, 0, 'C', 0);
}
?>